package com.youjia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youjia.mapper.RoleUserMapper;
import com.youjia.mapper.UserMapper;
import com.youjia.domain.RoleUser;
import com.youjia.domain.User;
import com.youjia.service.UserService;
import com.youjia.vo.PageResult;
import com.youjia.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Override
    public SysResult loginByUP(User user) {
        String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass);
        User userDB = userMapper.selectOne(new QueryWrapper<>(user));
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return userDB == null ? null : new SysResult(200,uuid, userDB);
    }
    @Override
    public PageResult getUserList(PageResult pageResult) {
        Page page = new Page(pageResult.getPageNum(), pageResult.getPageSize());
        page = userMapper.selectPage(page, new QueryWrapper().
                like(StringUtils.hasLength(pageResult.getQuery()), "username", pageResult.getQuery()));
        long total = page.getTotal();
        List<User> rows = page.getRecords();
        return pageResult.setRows(rows).setTotal(total);
    }

    @Override
    @Transactional
    public void updateStatus(User user) {
        userMapper.updateById(user);
    }

    @Override
    @Transactional
    public void addUser(User user) {
        user.setStatus(true).setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        userMapper.insert(user);
    }

    @Override
    @Transactional
    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("id", id));
    }

    @Override
    @Transactional
    public Boolean updatePwd(User user) {
        String newPwd = user.getPassword();
        user.setPassword(null);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
        User userDB = userMapper.selectOne(queryWrapper);
        if(userDB == null) {
            return false;
        }
        String md5Pass = DigestUtils.md5DigestAsHex(newPwd.getBytes());
        userDB.setPassword(md5Pass);
        userMapper.updateById(userDB);
        return true;
    }

    @Override
    @Transactional
    public Boolean insertUser(User user) {
        User userDB = userMapper.selectOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if(userDB != null) {
            return false;
        } else {
            String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
            user.setPassword(md5Pass).setStatus(true);
            userMapper.insert(user);
            Integer id = user.getId();
            RoleUser roleUser = new RoleUser();
            roleUser.setUserId(id).setRoleId(2);
            roleUserMapper.insert(roleUser);
            return true;
        }
    }
}
